package com.amazon.dao;

import com.amazon.bo.ShopClass;
import com.amazon.dao.base.BaseDao;
import org.hibernate.query.Query;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class ShopClassDao extends BaseDao<ShopClass> {
    //经营分类列表
    public List<ShopClass> getClassList(int shopid) {
        List<ShopClass> results = execute(session -> (List<ShopClass>) session
                .createNativeQuery("SELECT * FROM shop_class WHERE shop_id= "+shopid
                        +" ORDER BY id  ", ShopClass.class)
                .getResultList());
        return results != null ? results : new ArrayList<ShopClass>();
    }

    //店铺经营分类编辑
    public boolean  shopClassUpdate(ShopClass sc){
        try {
            ShopClass oldSc= getById(sc.getId());
            if(oldSc!=null){
                oldSc.setBrand(sc.getBrand());
                oldSc.setBulletPoint(sc.getBulletPoint());
                oldSc.setCompanyCode(sc.getCompanyCode());
                oldSc.setDescriptionLaster(sc.getDescriptionLaster());
                oldSc.setDescriptionPre(sc.getDescriptionPre());
                oldSc.setImportentNotice(sc.getImportentNotice());
                oldSc.setKeyWords(sc.getKeyWords());
                oldSc.setProductClassId(sc.getProductClassId());
                oldSc.setProductClassName(sc.getProductClassName());
                oldSc.setTitleLaster(sc.getTitleLaster());
                oldSc.setTitlePre(sc.getTitlePre());
                oldSc.setRatio(sc.getRatio());
                update(oldSc);
                return true;
            }
        }
        catch (Exception ex)
        {return false;}
        return false;
    }

    public ShopClass getShopClassByShopIdAndProductId(long shopId, long cId) {

        List<ShopClass> results = execute(session -> (List<ShopClass>) session
                .createNativeQuery("SELECT * FROM shop_class WHERE shop_id=:shopId and product_class_id=:cId "
                        +" ORDER BY id limit 1 ", ShopClass.class)
                .setParameter("shopId",shopId)
                .setParameter("cId",cId)
                .getResultList());
        return results.size()==0 ?null:results.get(0);
    }


}
